package com.lkx.web;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.lkx.util.ValidateCode;


@Controller
public class IndexController {


    @RequestMapping("/")
    public ModelAndView toIndex() {
        ModelAndView view = new ModelAndView("/back/login");
        return view;
    }

    @RequestMapping("/welcome")
    public ModelAndView welcome() {
        ModelAndView view = new ModelAndView("welcome");
        return view;
    }

    @RequestMapping("/list")
    public String index(String pageName) {
        return pageName == null ? "index" : pageName;
    }

    @RequestMapping("image")
    public void image(HttpServletResponse response, HttpServletRequest request) {

        /* 随机生成验证码位数 */
        Random rdm = new Random();
        int num = 1;
        response.setContentType("image/jpeg");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        ValidateCode vCode = new ValidateCode(100, 40, num, 20);
        HttpSession session = request.getSession();
        session.removeAttribute("validateCode");
        try {
            vCode.write(response.getOutputStream());
            session.setAttribute("validateCode", vCode.getCode().toLowerCase());
            vCode.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/test")
    @ResponseBody
    public Map ttest() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "0");
        map.put("msg", "success");
        map.put("data", null);
        return map;
    }

}
